// this.url = "http://api.icodeilife.cn:81/user";
class Register{
    constructor(){
        this.user=$("#user");
        this.pwd=$("#pwd");
        this.pwd2=$("#pwd2");
        this.submit=$(".submit");
        this.ps=$(".ps");
        this.span=$(".ps").find("span");
        this.addEvent();
        this.toast=$(".toast");
    }
    addEvent(){
        this.submit.on("click",()=>{
            this.u=this.user.val();
            this.p=this.pwd.val();
            this.p2=this.pwd2.val();

            this.Reg();
        })
    }
    Reg(){
        var length=/^.{8,16}$/;
        var a=0;
        var b=0;
        var c=0;
        
        var numReg=/\d/;
        if(numReg.test(this.p)){
            a=1;
        }
        var wordReg=/[a-zA-Z]/;
        if(wordReg.test(this.p)){
            b=1;
        }
        var otherReg=/[^\da-zA-Z]/;
        if(otherReg.test(this.p)){
            c=1;
        }
        console.log(a,b,c);
        console.log(this.p,this.p2);
        console.log(length.test(this.p))
        if(!length.test(this.p)||a+b+c<2||this.p!==this.p2){
            this.span.css("display","inline-block");
            this.ps.css("color","#ff6700");
            return;
        }else{
            this.go();
        }
        


    }
    go(){
        var that=this;
        $.ajax({
            url:"http://api.icodeilife.cn:81/user",
            data:{
                type: "register",
                user: this.u,
                pass: this.p
            },
            success:function(res){
                // {"type":"register","code":1,"msg":"注册成功"}
                if(JSON.parse(res).code==1){
                    that.toast.css({
                        "opacity":"1",
                        "background":"green"
                    });
                    that.toast.html(JSON.parse(res).msg);
                    var session={
                        user:that.u,
                        pwd:that.p
                    }
                    // sessionStorage.setItem("user",JSON.stringify(session));
                    setTimeout(() => {
                        that.toast.css({
                            "opacity":"0"
                        });
                    
                        location.href="login.html";
                    }, 2000);

                }else{
                    that.toast.css({
                        "opacity":"1",
                        "background":"red"
                    });
                    that.toast.html(JSON.parse(res).msg);
                    setTimeout(() => {
                        that.toast.css({
                            "opacity":"0"
                        });
                        
                    }, 2000);

                   

                }
            }
        })

    }

}
new Register();
